const initState = {
    result: ''
}

export const actionTypes = {
    DUCKS_PENDING: 'DUCKS_PENDING',
    DUCKS_DONE: 'DUCKS_DONE',
    DUCKS_FAIL: 'DUCKS_FAIL'
}

export const actions = {
    getDucks: (cityId) => {
        return {
            promise: fetch(`/data/cityinfo/${cityId}.html`).then(res => {
                if(res.status !== 200){
                    return console.log(`Fail get response with status ${res.status}`)
                }

                return res.json().then(res => res.weatherinfo)
            }),
            types: [actionTypes.DUCKS_PENDING, actionTypes.DUCKS_DONE, actionTypes.DUCKS_FAIL]
        }
    }
}

export const reducer = (state=initState, action) => {
    switch(action.type) {
        case 'DUCKS_DONE':
        return {
            ...state,
            result: action.payload
        }

        case 'DUCKS_FAIL':
        return {
            ...state,
            result: action.payload
        }

        default:
        return state
    }
}
